package cn.buk.tms.eotms.dto;

import cn.buk.tms.common.dto.base.BasePassengerDto;
import cn.buk.tms.eotms.entity.Passenger;
import cn.buk.tms.eotms.entity.PassengerFfpNo;
import cn.buk.tms.eotms.entity.PassengerIdInfo;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yfdai
 */
@JsonPropertyOrder({"id", "name", "englishName", "firstName", "lastName", "gender", "birthday", "email", "phone", "mobile"})
public class PassengerDto extends BasePassengerDto {

    private int id;

    /**
     * 中文姓名
     */
    private String name;

    /**
     * 英文姓名
     */
    private String englishName;

    /**
     * Given Name 名
     */
    private String firstName;

    /**
     * Sur name 名
     */
    private String lastName;

    @JsonIgnore
    private String nameCn;

    @JsonIgnore
    private String nameEn;

    /**
     * 部门名称
     */
    private String departmentName;

    /**
     * 部门id
     */
    private int departmentId;

    /**
     * 职位名称
     */
    private String positionName;

    /**
     * 职位id
     */
    private int positionId;


    private Integer gender;

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date birthday;

    /**
     * 证件信息
     */
    private List<PassengerIdInfoDto> idInfos;

    /**
     * 常旅客卡号信息
     */
    private List<PassengerFfpNoDto> ffpNos;

    private String nationality;

    private String email;

    private String mobile;

    private String phone;

    private String fax;

    /**
     * 机上座位的喜好（前排，后排，中部？靠窗？靠走道？）
     */
    private String hobby;

    /**
     * 常旅客卡号
     */
    @Deprecated
    @JsonIgnore
    private String ffpNo;

    /**
     * 可以乘坐的飞机等级
     */
    @JsonIgnore
    private String flightLevel;

    /**
     * 可以乘坐的火车等级
     */
    @JsonIgnore
    private String trainLevel;

    /**
     * VIP级别
     */
    private int vipLevel;

    private String remark;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date lastUpdate;

    public static PassengerDto createBy(Passenger passenger) {
        PassengerDto dto = new PassengerDto();
        BeanUtils.copyProperties(passenger, dto, "idInfos", "ffpNos");

        dto.setName(passenger.getNameCn());
        dto.setEnglishName(passenger.getNameEn());

        dto.setIdInfos(new ArrayList<>());
        for(var idInfo: passenger.getIdInfos()) {
            var idInfoDto = new PassengerIdInfoDto();
            BeanUtils.copyProperties(idInfo, idInfoDto);
            idInfoDto.setId(null);

            dto.getIdInfos().add(idInfoDto);
        }
        if (dto.getIdInfos().size() == 0) {
            //兼容之前的保存方式

            //身份证
            if (passenger.getIdNo() != null && passenger.getIdNo().trim().length() > 0) {
                PassengerIdInfoDto idInfoDto = new PassengerIdInfoDto();
                idInfoDto.setIdType(1);
                idInfoDto.setIdNo(passenger.getIdNo().trim());

                dto.getIdInfos().add(idInfoDto);
            }
            //护照
            if (passenger.getPassportNo() != null && passenger.getPassportNo().trim().length() > 0) {
                PassengerIdInfoDto idInfoDto = new PassengerIdInfoDto();
                idInfoDto.setIdType(2);
                idInfoDto.setIdNo(passenger.getPassportNo().trim());
                idInfoDto.setIdIssueNation(passenger.getNationality());
                idInfoDto.setIdValidDate(passenger.getPassportExpiry());

                dto.getIdInfos().add(idInfoDto);
            }
        }

        //常旅客卡号信息
        for(PassengerFfpNo ffp: passenger.getFfpNos()) {
            PassengerFfpNoDto ffpDto = new PassengerFfpNoDto();
            BeanUtils.copyProperties(ffp, ffpDto);

            dto.getFfpNos().add(ffpDto);
        }

        if (passenger.getDepartment() != null) {
            dto.setDepartmentName(passenger.getDepartment().getName());
            dto.setDepartmentId(passenger.getDepartment().getId());
        }

        if (passenger.getPosition() != null) {
            dto.setPositionId(passenger.getPosition().getId());
            dto.setPositionName(passenger.getPosition().getName());

            if (passenger.getVipLevel() < 1 && passenger.getPosition().getVipLevel() > 0) {
                dto.setVipLevel(passenger.getPosition().getVipLevel());
                dto.setRemark(dto.getRemark() + " 使用默认VIP级别");
            }
        }

        return dto;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNameCn() {
        return nameCn;
    }

    public void setNameCn(String nameCn) {
        this.nameCn = nameCn;
    }

    public String getNameEn() {
        return nameEn;
    }

    public void setNameEn(String nameEn) {
        this.nameEn = nameEn;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
//
//    public String getIdNo() {
//        return idNo;
//    }
//
//    public void setIdNo(String idNo) {
//        this.idNo = idNo;
//    }
//
//    public String getPassportNo() {
//        return passportNo;
//    }
//
//    public void setPassportNo(String passportNo) {
//        this.passportNo = passportNo;
//    }
//
//    public Date getPassportExpiry() {
//        return passportExpiry;
//    }
//
//    public void setPassportExpiry(Date passportExpiry) {
//        this.passportExpiry = passportExpiry;
//    }

    public String getNationality() {
        return nationality;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public String getFfpNo() {
        return ffpNo;
    }

    public void setFfpNo(String ffpNo) {
        this.ffpNo = ffpNo;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    public String getFlightLevel() {
        return flightLevel;
    }

    public void setFlightLevel(String flightLevel) {
        this.flightLevel = flightLevel;
    }

    public String getTrainLevel() {
        return trainLevel;
    }

    public void setTrainLevel(String trainLevel) {
        this.trainLevel = trainLevel;
    }

    public List<PassengerIdInfoDto> getIdInfos() {
        return idInfos;
    }

    public void setIdInfos(List<PassengerIdInfoDto> idInfos) {
        this.idInfos = idInfos;
    }

    public int getVipLevel() {
        return vipLevel;
    }

    public void setVipLevel(int vipLevel) {
        this.vipLevel = vipLevel;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public int getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(int departmentId) {
        this.departmentId = departmentId;
    }

    public String getPositionName() {
        return positionName;
    }

    public void setPositionName(String positionName) {
        this.positionName = positionName;
    }

    public int getPositionId() {
        return positionId;
    }

    public void setPositionId(int positionId) {
        this.positionId = positionId;
    }

    public String getEnglishName() {
        return englishName;
    }

    public void setEnglishName(String englishName) {
        this.englishName = englishName;
    }

    public List<PassengerFfpNoDto> getFfpNos() {
        if (ffpNos == null) {
            ffpNos = new ArrayList<>();
        }
        return ffpNos;
    }

    public void setFfpNos(List<PassengerFfpNoDto> ffpNos) {
        this.ffpNos = ffpNos;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}
